package com.igeek.web;

import com.igeek.domain.*;
import com.igeek.service.OrderService;
import com.igeek.utils.CommonUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@WebServlet("/order")
public class OrderServlet extends BaseServlet {

    /**
     * 提交订单
     * @param request
     * @param response
     */
    public void submitOrder(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        //验证是否有用户登录
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");
        if(user==null){
            //打回到登录页面进行登录
            response.sendRedirect("login.jsp");
            return;
        }
        //再进行订单的处理
        Order order=new Order();
        //订单的编号
        String oid=CommonUtils.getUUID();
        order.setOid(oid);
        //订单时间
        order.setOrdertime(new Date());
        Cart cart=(Cart) session.getAttribute("cart");
        //订单总金额
        order.setTotal(cart.getTotal());
        //是哪个用户下的订单
        order.setUser(user);
        //订单详情
        Map<String, CartItem> map=cart.getMap();
        List<OrderItem> list=new ArrayList<>();
        for(String key:map.keySet()){
            //购物车中的购物项转换成订单中的订单详情信息
            CartItem item=map.get(key);
            OrderItem orderItem=new OrderItem();
            orderItem.setItemid(CommonUtils.getUUID());
            orderItem.setCount(item.getCount());
            orderItem.setSubtotal(item.getSubTotal());
            orderItem.setProduct(item.getProduct());
            orderItem.setOid(oid);
            //存入集合
            list.add(orderItem);
        }
        //存放到订单对象中
        order.setOrderItems(list);
        OrderService orderService=new OrderService();
        boolean result=orderService.submitOrder(order);
        if(result){
            //清空购物车
            session.removeAttribute("cart");
//            request.setAttribute("order",order);
            session.setAttribute("order",order);
//            request.getRequestDispatcher("/order_info.jsp").forward(request,response);
            response.sendRedirect("order_info.jsp");
        }else{
            request.setAttribute("msg","操作系统异常");
            request.getRequestDispatcher("error.jsp").forward(request,response);
        }
    }

    /**
     * 查询所有订单
     * @param request
     * @param response
     */
    public void myOrders(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        OrderService orderService=new OrderService();
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");
        if(user==null){
            response.sendRedirect("login.jsp");
            return;
        }
        String uid=user.getUid();
        List<Order> orders=orderService.getAllOrder(uid);
        request.setAttribute("orders",orders);
        request.getRequestDispatcher("/order_list.jsp").forward(request,response);
    }

    /**
     * 查询某个订单信息
     */
    public void myOrderDetail(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String oid=request.getParameter("oid");
        OrderService orderService=new OrderService();
        Order order=orderService.getOrderDetail(oid);
        HttpSession session=request.getSession();
        /*request.setAttribute("order",order);
        request.getRequestDispatcher("/order_info.jsp").forward(request,response);*/
        session.setAttribute("order",order);
        response.sendRedirect("order_info.jsp");
    }

    public void updateOrder(HttpServletRequest request,HttpServletResponse response) throws IOException {
        HttpSession session=request.getSession();
        Order order=(Order) session.getAttribute("order");
        try {
            BeanUtils.populate(order,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(order);
        OrderService orderService=new OrderService();
        orderService.updateOrder(order);
        //做页面的跳转
        response.sendRedirect("alipay.jsp");
    }

}
